<?php
// +----------------------------------------------------------------------
// | Yzncms [ 御宅男工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 御宅男 <530765310@qq.com>
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// | 后台用户管理
// +----------------------------------------------------------------------
namespace app\admin\model;

use app\admin\service\User;
use app\api\model\Order;
use think\Exception;
use think\Model;

class Area extends Model
{
    protected $autoWriteTimestamp = true;
    protected $updateTime         = false;



    public function getAreaList(){
        $list = self::where(['status' => 1])->field('id,name,sort')->order('sort desc')->select()->toArray();
        foreach ($list as $key=>&$value){
            $num = (new Car())->where(['aid' => $value['id'],'status' => 1])->count();
            $makeCarCount = (new Order())->where(['aid' => $value['id'],'status' => [0,1]])->count();
            $value['stock'] = ($num - $makeCarCount) > 0 ? $num - $makeCarCount:0;

            if($value['stock'] == 0){
                unset($list[$key]);
            }
        }


        ksort($list);


        $web_close_order = (new Config())->where(['id' => 15])->field('value')->find();
        $makeTime = date('Y-m-d H:i:s',time()+$web_close_order['value']*60);
        return ['code' => 200, 'msg' => '获取信息成功', 'data' => ['list' => $list,'makeTime' => $makeTime]];
    }




}
